package com.mcxx.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class JsonUtil {
    private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);


    public static Map<String,Object> jsonToMap(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String,Object> map = null;
        try {
            map = (Map<String,Object>)objectMapper.readValue(json, Map.class);
        } catch (JsonParseException e) {
            logger.info(e.getMessage(), e);
        } catch (JsonMappingException e) {
            logger.info(e.getMessage(), e);
        } catch (IOException e) {
            logger.info(e.getMessage(), e);
        }
        return map;
    }


    public static List<Map<String,?>> jsonToMapList(String json) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Map.class);
        List<Map<String, ?>> mapList;
        try {
            mapList = (List<Map<String,?>>)objectMapper.readValue(json, javaType);
        } catch (JsonParseException e) {
            logger.info(e.getMessage(), e);
            throw e;
        } catch (JsonMappingException e) {
            logger.info(e.getMessage(), e);
            throw e;
        } catch (IOException e) {
            logger.info(e.getMessage(), e);
            throw e;
        }
        return mapList;
    }


    public static String mapToJson(Map<String,Object> map){
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = null;
        try {
            jsonString = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            logger.info(e.getMessage(), e);
        }
        return jsonString;
    }


    public static String objectToJson(Object object) {
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonList = null;
        try {
            jsonList = objectMapper.writeValueAsString(object);
        } catch (JsonParseException e) {
            logger.info(e.getMessage(), e);
        } catch (JsonMappingException e) {
            logger.info(e.getMessage(), e);
        } catch (IOException e) {
            logger.info(e.getMessage(), e);
        }
        return jsonList;
    }


    public static String mapListToJson(List<Map<String, Object>> cardInfoList) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonList;
        try {
            jsonList = objectMapper.writeValueAsString(cardInfoList);
        } catch (JsonParseException e) {
            logger.info(e.getMessage(), e);
            throw e;
        } catch (JsonMappingException e) {
            logger.info(e.getMessage(), e);
            throw e;
        } catch (IOException e) {
            logger.info(e.getMessage(), e);
            throw e;
        }
        return jsonList;
    }


    public static String arrayToJson(String[] args) throws Exception{

        Map<String,String> map = new HashMap<String,String>();
        for(int i=0; i<args.length; i++){
            map.put(i+"", args[i]);
        }
        String json = JsonUtil.objectToJson(map);
        return json;
    }


    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            T t = objectMapper.readValue(jsonData, beanType);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static <T> List<T> jsonToList(String jsonData, Class<T> clazz) {
        List<T> ts = (List<T>)JSONArray.parseArray(jsonData,clazz);
        return ts;
    }

}

